//
//  DrawerManager.m
//  Drawer
//
//  Created by Young on 16/8/13.
//  Copyright © 2016年 Smile. All rights reserved.
//

#import "DrawerManager.h"

#import "UIView+Supplement.h"

@interface DrawerManager ()

@property (weak, nonatomic) UIViewController *centerViewController;
@property (weak, nonatomic) UIView *leftView;
@property (strong, nonatomic) UIPanGestureRecognizer *pan;

@end

@implementation DrawerManager
/**
 *  抽屉管理类
 */
+ (instancetype)sharedDrawerManager {
    static DrawerManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [[DrawerManager alloc] init];
        }
    });
    return manager;
}

- (void)installCenterViewController:(UIViewController *)centerViewController leftView:(UIView *)leftView {
    self.centerViewController = centerViewController;
    self.leftView = leftView;
    
    [[UIApplication sharedApplication].delegate window].rootViewController = self.centerViewController;
    [[[UIApplication sharedApplication].delegate window] addSubview:leftView];
    [self showSideShadow];
    [self.centerViewController.view addGestureRecognizer:self.pan];
}

- (void)showSideShadow {
    if (!self.centerViewController.view) {
        return;
    }
    self.centerViewController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.centerViewController.view.layer.shadowOffset = CGSizeMake(6, 6);
    self.centerViewController.view.layer.shadowOpacity = 0.7;
    self.centerViewController.view.layer.shadowRadius = 6.0f;
}

- (void)hiddenSideShadow {
    if (!self.centerViewController.view) {
        return;
    }
    self.centerViewController.view.layer.shadowColor = [UIColor whiteColor].CGColor;
    self.centerViewController.view.layer.shadowOffset = CGSizeMake(0, 0);
    self.centerViewController.view.layer.shadowOpacity = 0;
    self.centerViewController.view.layer.shadowRadius = 0;
}

- (void)beginDragResponse {
    if (!self.centerViewController.view) {
        return;
    }
    [self.centerViewController.view addGestureRecognizer:self.pan];
}

- (void)cancelDragResponse {
    if (!self.centerViewController.view) {
        return;
    }
    [self.centerViewController.view removeGestureRecognizer:self.pan];
}

- (void)setShowType:(DrawerManagerShowType)showType {
    if (!self.centerViewController.view) {
        return;
    }
    CGAffineTransform rightScopeTransform = CGAffineTransformTranslate([[UIApplication sharedApplication].delegate window].transform, [UIScreen mainScreen].bounds.size.width * kLeftWidthScale, 0);
    switch (showType) {
        case DrawerManagerShowTypeLeft: {
            [UIView animateWithDuration:0.2f animations:^{
                self.centerViewController.view.transform = CGAffineTransformIdentity;
                [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = self.centerViewController.view.tx / 3;
            }];
            break;
        }
        case DrawerManagerShowTypeCenter: {
            [UIView animateWithDuration:0.2f animations:^{
                self.centerViewController.view.transform = CGAffineTransformIdentity;
                [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = self.centerViewController.view.tx / 3;
            }];
            break;
        }
        case DrawerManagerShowTypeLeftWithoutAnimation: {
            self.centerViewController.view.transform = rightScopeTransform;
            [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = self.centerViewController.view.tx / 3;
            break;
        }
        case DrawerManagerShowTypeCenterWithoutAnimation: {
            self.centerViewController.view.transform = CGAffineTransformIdentity;
            [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = self.centerViewController.view.tx / 3.0;
            break;
        }
    }
}

- (UIPanGestureRecognizer *)pan {
    if (!_pan) {
        _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
    }
    return _pan;
}

- (void)handlePanAction:(UIPanGestureRecognizer *)sender {
    // 1. 获取手指拖拽的时候，平移的值
    CGPoint translation = [sender translationInView:sender.view];
    // 2. 让当前视图进行平移
    sender.view.transform = CGAffineTransformTranslate(sender.view.transform, translation.x, 0);
    [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = sender.view.tx / 3;
    // 3. 让平移的值不要累加
    [sender setTranslation:CGPointZero inView:sender.view];
    // 4. 获取最右边的范围
    CGAffineTransform rightScopeTransform = CGAffineTransformTranslate([[UIApplication sharedApplication].delegate window].transform, [UIScreen mainScreen].bounds.size.width * kLeftWidthScale, 0);
    
    if (sender.view.tx > rightScopeTransform.tx) {
        // 当移动到右边极限时
        // 限制最右边的范围
        sender.view.transform = rightScopeTransform;
        [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = sender.view.tx / 3;
    } else if (sender.view.tx < 0.0) {
        // 限制最左边的范围
        sender.view.transform = CGAffineTransformTranslate([[UIApplication sharedApplication].delegate window].transform, 0, 0);
        [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = sender.view.tx / 3;
    }
    
    // 拖拽结束时
    if (sender.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.2f animations:^{
            if (sender.view.left > [UIScreen mainScreen].bounds.size.width * 0.5) {
                sender.view.transform = rightScopeTransform;
                [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = sender.view.tx / 3;
            } else {
                sender.view.transform = CGAffineTransformIdentity;
                [[UIApplication sharedApplication].delegate window].subviews.firstObject.tx = sender.view.tx / 3;
            }
        }];
    }
}

@end
